package net.cloudcodex.client.home.presenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.cloudcodex.client.CloudCodexEventBus;
import net.cloudcodex.client.campaign.presenter.CampaignPresenter;
import net.cloudcodex.client.character.presenter.CharacterPresenter;
import net.cloudcodex.client.home.view.HomeCampaignView;
import net.cloudcodex.client.home.view.HomeCharacterView;
import net.cloudcodex.client.home.view.HomeView;
import net.cloudcodex.client.home.view.HomeView.Message;
import net.cloudcodex.client.home.view.HomeViewImpl;
import net.cloudcodex.shared.dto.NotificationDTO;
import net.cloudcodex.shared.dto.home.CampaignSummaryDTO;
import net.cloudcodex.shared.dto.home.CharacterSummaryDTO;
import net.cloudcodex.shared.dto.home.HomeDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * 
 * 
 * @author ADaroussin
 */
@Presenter(view = HomeViewImpl.class)
public class HomePresenter extends BasePresenter<HomeView, CloudCodexEventBus> {
	private final Message message;
	private HomeDTO home;
	private Map<Long, HomeCharacterPresenter> listCharacterPresenter = new HashMap<Long, HomeCharacterPresenter>();
	private Map<Long, HomeCampaignPresenter> listCampaignPresenter = new HashMap<Long, HomeCampaignPresenter>();
	

	@Inject
	public HomePresenter(Message pMessage) {
		this.message = pMessage;
	}

	@Override
	public void bind() {
		this.view.getAddCampaignButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				eventBus.createNewCampaign(home.getNickname());
				
			}
		});

	}



	private void displayHome() {

		view.initHomePage();
		view.addNickName(home.getNickname());

		final List<NotificationDTO> notifications = home.getNotifications();
		if (notifications != null) {
			for (NotificationDTO notificationDTO : notifications) {
				view.addNotifications(notificationDTO.getMessage(),notificationDTO.getDate());
				
			}
		}
		final List<NotificationDTO> userNotifications = home.getUserNotifications();
		if (userNotifications != null) {
			for (NotificationDTO notificationDTO : userNotifications) {
				view.addNotifications(notificationDTO.getMessage(),notificationDTO.getDate());
				
			}
		}

		final List<CharacterSummaryDTO> characters = home.getCharacters();
		if (characters != null) {
			
			for (CharacterSummaryDTO characterSummaryDTO : characters) {
				initCharacterView(characterSummaryDTO);
				//				listCharacterView.add(homeCharacterPresenter.getView());
			}
		}
		
		final List<CampaignSummaryDTO> campaigns = home.getCampaigns();
		if (campaigns != null) {
			for (CampaignSummaryDTO campaignSummaryDTO : campaigns) {
				initCampaignView(campaignSummaryDTO);
			}
		}
		// final List<CampaignInvitationDTO> invitations =
		// home.getInvitations() ;
		// view.addInvitations(invitations);
		// final List<CampaignRegistrationDTO> inscriptions =
		// home.getInscriptions();
		// view.addInscriptions(inscriptions);
		
		view.initView();
	}

	/**
	 * @param characterSummaryDTO
	 */
	public void initCharacterView(CharacterSummaryDTO characterSummaryDTO) {
		HomeCharacterPresenter homeCharacterPresenter = eventBus.addHandler(HomeCharacterPresenter.class);
		listCharacterPresenter.put(characterSummaryDTO.getId(), homeCharacterPresenter); 
		homeCharacterPresenter.addCharacterSummary(characterSummaryDTO);
		homeCharacterPresenter.initView();
		view.addChararacterView(homeCharacterPresenter.getView());
	}

	/**
	 * @param campaignSummaryDTO
	 */
	public void initCampaignView(CampaignSummaryDTO campaignSummaryDTO) {
		HomeCampaignPresenter homeCampaignPresenter = eventBus.addHandler(HomeCampaignPresenter.class);
		listCampaignPresenter.put(campaignSummaryDTO.getId(), homeCampaignPresenter);
		homeCampaignPresenter.addCampaignSummaryDTO(campaignSummaryDTO);
		homeCampaignPresenter.initView();
		view.addCampaignView(homeCampaignPresenter.getView());
//		listCampaignView.add(homeCampaignPresenter.getView());
	}


	public void onInitHomeTab(HomeDTO homeDTO) {
		this.home = homeDTO;
		displayHome();
		eventBus.addTab(view, this.message.getHomeTabName());
		eventBus.selectTab(view);
	}
	
	public void onAddNewCampaign(CampaignSummaryDTO campaignSummaryDTO){
		this.initCampaignView(campaignSummaryDTO);
	}

	public void onUpdateCampaign(CampaignSummaryDTO campaignSummaryDTO) {
		
		HomeCampaignPresenter campaignPresenter = listCampaignPresenter.get(campaignSummaryDTO.getId());
		
		campaignPresenter.addCampaignSummaryDTO(campaignSummaryDTO);
		campaignPresenter.refreshCampaignConfig();
	}

}
